home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / comm / misc / Sashi89.lha / Sashi89 / sources / list.c < prev    next >
C/C++ Source or Header  |  2001-05-05  |  2KB  |  105 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #include "list.h"
  6.  
  7.  
  8. void List_Init(struct List_List *list)
  9. {
  10.   list->first  = NULL;
  11.   list->last   = NULL;
  12. }
  13.  
  14. void *List_Push(struct List_List *list, size_t size)
  15. {
  16.   struct List_Node  *new;
  17.   void              *content;
  18.  
  19.   new     = malloc(sizeof(struct List_Node));
  20.   content = malloc(size);
  21.  
  22.   if ( ! new || ! content )
  23.   {
  24.     free(new);
  25.     free(content);
  26.     return(NULL);
  27.   }
  28.  
  29.   new->prev = NULL;
  30.   new->next = list->first;
  31.  
  32.  
  33.   if ( list->first == NULL )
  34.     list->last = new;
  35.   else
  36.     list->first->prev = new;
  37.  
  38.   list->first = new;
  39.  
  40.   printf("new=%p\n",new);
  41.   printf("content=%p\n",content);
  42.  
  43.   new->content = content;
  44.   return(content);
  45. }
  46.  
  47. void *List_Top(struct List_List *list)
  48. {
  49.   if ( list->first == NULL )
  50.     return(NULL);
  51.  
  52.   return(list->first->content);
  53. }
  54.  
  55. void List_Pop(struct List_List *list)
  56. {
  57.   struct List_Node *newfirst;
  58.  
  59.   if ( ! list->first )
  60.     return;
  61.  
  62.   newfirst = list->first->next;
  63.  
  64.   free(list->first->content);
  65.   free(list->first);
  66.  
  67.   list->first = newfirst;
  68. }
  69.  
  70. void List_Invert(struct List_List *list)
  71. {
  72.   struct List_Node *temp;
  73.   struct List_Node *current;
  74.  
  75.   current = list->first;
  76.   while ( current )
  77.   {
  78.     temp          = current->next;
  79.     current->next = current->prev;
  80.     current->prev = temp;
  81.  
  82.     current = temp;
  83.   }
  84.  
  85.   temp        =   list->first;
  86.   list->first =   list->last;
  87.   list->last  =   temp;
  88. }
  89.  
  90. /* Hack */
  91. void List_Print(struct List_List *list)
  92. {
  93.   struct List_Node *current;
  94.  
  95.   current = list->first;
  96.   while ( current )
  97.   {
  98.     printf("current = %p\n",current);
  99.     printf("content = %p\n",current->content);
  100.     printf("N%d\n",*((int*)current->content));
  101.     current       = current->next;
  102.   }
  103.  
  104. }
  105.